import { SOCKET_ADDRESS } from "../config";
import { NSystem, RSetState } from "../config/constants";
import { reducer } from "./dva17";
import createSocket from "socket.io-client";

// 显示toast
export function showToast(msg: string, duration = 2000) {
  reducer(NSystem, RSetState, { msg });
  setTimeout(() => {
    reducer(NSystem, RSetState, { msg: "" });
  }, duration);
}
// 手机端检测
export const isMobile = (function () {
  let userAgentInfo = navigator.userAgent;
  let mobileAgents = [
    "Android",
    "iPhone",
    "SymbianOS",
    "Windows Phone",
    "iPad",
    "iPod",
  ];
  let mobile_flag = false;

  //根据userAgent判断是否是手机
  for (let v = 0; v < mobileAgents.length; v++) {
    if (userAgentInfo.indexOf(mobileAgents[v]) > 0) {
      mobile_flag = true;
      break;
    }
  }
  let screen_width = window.screen.width;
  let screen_height = window.screen.height;
  //根据屏幕分辨率判断是否是手机
  if (screen_width < 500 && screen_height < 800) {
    mobile_flag = true;
  }
  return mobile_flag;
})();

// 处理浮点数
export const toFixed = (num: number, digit = 3) => {
  const scale = 10 ** digit;
  return Math.floor(num * scale) / scale;
};

// 初始化socket
export const socket = (() => createSocket(SOCKET_ADDRESS))();

// 2个整数范围内随机
export const rand = (min, max) =>
  Math.floor((max - min + 1) * Math.random()) + min;

export const players = new Map();
